
package protocol.transactions;

import java.util.LinkedList;

import protocol.XoneCallBack;
import protocol.XoneProtocol;
import protocol.XoneService;
import protocol.XoneServiceCode;
import protocol.meta.User;

import com.google.gson.JsonElement;
import com.netease.http.THttpRequest;
import com.netease.log.NTLog;
import com.netease.pkgRelated.ErrorToString;
import com.netease.pkgRelated.XoneConsts.ACCOUNT_TYPE;
import com.netease.task.Transaction;

import db.manager.ManagerAccount;
import db.manager.ManagerAccount.Account;

public class LoginTransaction extends EncryptBaseTransaction {

    int mType;
    String mUserName;
    String mPwd;

    public LoginTransaction(int type, String username, String pw) {
        super(TRANSACTION_TYPE_LOGIN);
        mType = type;
        mUserName = username;
        mPwd = pw;
    }

    @Override
    protected void onXoneTransactionSuccess(int code, Object obj) {
        User user = null;
        if (obj != null && obj instanceof JsonElement) {
            user = User.fromJson((JsonElement)obj);
        }
        if (user != null) {
            XoneService.getInstance().setIsLoging(false);
            Account acc = new Account();
            if (mType != ACCOUNT_TYPE.Anonymous) {
                acc.mUserName = mUserName;
                acc.mPassword = mPwd;
            } else {
                acc.mUserName = ManagerAccount.NAME_ANONUMOUS;
                acc.mPassword = ManagerAccount.PASSWORD_ANONUMOUS;
            }
            acc.mUserId = user.userId;
            acc.mNickName = user.nickname;
            acc.mUserType = user.loginType;
            acc.mIsLastLogin = true;
            if (user.avatar != null) {
                acc.mAvatar = user.avatar;
            }
            acc.mAttr = user.userAttr;
            acc.mAvatar = user.avatar;
            ManagerAccount.getInstance().setLoginAccount(acc);
            notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, user);
            LinkedList<Transaction> list = XoneService.getInstance().getWattingList();
            for (Transaction tx : list) {
                if (tx != null) {
                    getTransactionEngine().beginTransaction(tx);
                }
            }
            XoneService.getInstance().doSyncBindingInfo(mUserName);
            XoneService.getInstance().doGetAppSetting();
        } else {
            notifyError(XoneServiceCode.ERR_CODE_DATA_PARSE_EXCEPTION,
                    ErrorToString.getString(XoneServiceCode.ERR_CODE_DATA_PARSE_EXCEPTION));
        }
    }

    @Override
    protected void onXoneTransactionError(int errCode, Object obj) {
        notifyError(errCode, obj);
        XoneService.getInstance().setIsLoging(false);
    }

    private XoneCallBack mGetIdKeyCallback = new XoneCallBack() {

        @Override
        public void onGetIdKey(int transactionId) {
            NTLog.i("LoginTransaction", "onGetIdKey");
            getTransactionEngine().beginTransaction(LoginTransaction.this);
            XoneService.getInstance().removeListener(mGetIdKeyCallback);
        };

        @Override
        public void onGetIdKeyError(int transactionId, int errCode, String err) {
            notifyError(errCode, err);
            XoneService.getInstance().removeListener(mGetIdKeyCallback);
            LoginTransaction.this.doEnd();
        };
    };

    @Override
    void realOntransact() {
        THttpRequest request = XoneProtocol.getInstance()
                .createLoginRequest(mType, mUserName, mPwd);
        sendRequest(request);

    }

}
